//
//  DHHomeCouponCell.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/4/13.
//  Copyright © 2018年 张朋. All rights reserved.
//

#pragma mark - CouponItemView
@interface CouponItemView : UIView

@property (strong, nonatomic) UILabel *nameLabel; //名字
@property (strong, nonatomic) UILabel *disLabel; //描述
@property (strong, nonatomic) UILabel *timeLabel; //到期时间

@end

@implementation CouponItemView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        UIImageView *leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(-4, 0, 7, frame.size.height)];
        leftImageView.image = [UIImage imageNamed:@"coupon_left"];
        leftImageView.contentMode = UIViewContentModeScaleToFill;
        [self addSubview:leftImageView];
        
        UIImageView *rightImageView = [[UIImageView alloc] initWithFrame:CGRectMake(frame.size.width - 3, 0, 7, frame.size.height)];
        rightImageView.image = [UIImage imageNamed:@"coupon_right"];
        rightImageView.contentMode = UIViewContentModeScaleToFill;
        [self addSubview:rightImageView];
        
        UIImageView *card = [[UIImageView alloc] initWithFrame:CGRectMake(frame.size.width-50, 0, 40, frame.size.height)];
        card.image = [UIImage imageNamed:@"coupon_card"];
        card.contentMode = UIViewContentModeScaleAspectFit;
        [self addSubview:card];
        
        if (!self.nameLabel) {
            self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(6, 5, frame.size.width - 4, 20)];
            self.nameLabel.font = [UIFont systemFontOfSize:15];
            self.nameLabel.textColor = [UIColor whiteColor];
            [self addSubview:self.nameLabel];
        }
        if (!self.disLabel) {
            self.disLabel = [[UILabel alloc] initWithFrame:CGRectMake(6, self.nameLabel.bottom, frame.size.width - 4, 15)];
            self.disLabel.font = [UIFont systemFontOfSize:11];
            self.disLabel.textColor = [UIColor whiteColor];
            [self addSubview:self.disLabel];
        }
        if (!self.timeLabel) {
            self.timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(6, self.disLabel.bottom, frame.size.width - 4, 10)];
            self.timeLabel.font = [UIFont systemFontOfSize:10];
            self.timeLabel.textColor = [UIColor whiteColor];
            [self addSubview:self.timeLabel];
        }
    }
    return self;
}

@end


#import "DHHomeCouponCell.h"
#import "DHMyCouponsController.h"

@implementation DHHomeCouponCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.scrollView.userInteractionEnabled = YES;
    [self.scrollView bk_whenTapped:^{
        DHMyCouponsController *vc = [[DHMyCouponsController alloc] init];
        vc.hidesBottomBarWhenPushed = YES;
        [self.viewController.navigationController pushViewController:vc animated:YES];
    }];
}

- (void)updateCellWithArray:(NSArray *)arr{
    CGFloat itemW = 134;
    CGFloat spaceX = 10;
    self.scrollView.contentSize = CGSizeMake((itemW + spaceX) * arr.count, self.scrollView.height);
    for (int i = 0; i < arr.count; i++) {
        
        NSDictionary *model = arr[i];
        CouponItemView *itemView = [[CouponItemView alloc] initWithFrame:CGRectMake((itemW + spaceX) * i, 0, itemW, self.scrollView.height)];
        itemView.backgroundColor = [UIColor colorWithHexString:[model objectForKey:@"color"]];
        itemView.nameLabel.text = [model objectForKey:@"name"];
        
        NSDate *date = [NSDate dateWithServerISOString:[model objectForKey:@"expire"] format:@"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
        
        itemView.timeLabel.text = [NSString stringWithFormat:@"有效期至%@",[NSDate stringFromDate:date withFormat:@"yyyy/MM/dd"]];
        itemView.disLabel.text = [model objectForKey:@"type"];
        [self.scrollView addSubview:itemView];
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
